package com.piece.core.framework.util.security;

import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.framework.util.security.coder.*;
import java.util.Random;

/**
 * 数据加密辅助类
 */
public class SecurityUtil {

    public static final String UTF8 = "UTF-8";

    /**
     * BASE64解码
     */
    public static String decryptBASE64(String base64Code) {
        return StringUtil.isEmpty(base64Code) ? null : Base64Coder.decode(base64Code);
    }

    /**
     * BASE64解码
     */
    public static byte[] decryptBASE64ToByte(String base64Code) throws Exception {
        return StringUtil.isEmpty(base64Code) ? null : Base64Coder.decode(base64Code).getBytes();
    }

    /**
     * BASE64编码
     */
    public static String encryptBASE64(String data) {
        return Base64Coder.encode(data);
    }

    /**
     * BASE64编码
     */
    public static String encryptBASE64(byte[] bytes) {
        return Base64Coder.encode(bytes);
    }

    /**
     * DES解码
     */
    public static String decryptDes(String cryptData) throws Exception {
        return new String(DESCoder.decrypt(decryptBASE64(cryptData).getBytes()));
    }

    /**
     * DES编码
     */
    public static String encryptDes(String data) throws Exception {
        return encryptBASE64(DESCoder.encrypt(data.getBytes()));
    }

    /**
     * AES编码
     */
    public static final String encryptAes(String data) throws Exception {
        return AESCoder.aesEncrypt(data);
    }

    /**
     * AES解码
     */
    public static String decryptAes(String cryptData) throws Exception {
        return AESCoder.aesDecrypt(cryptData);
    }

    /**
     * 基于MD5算法的单向加密
     */
    public static String encryptMd5(String data) {
        return MD5Coder.decode(data);
    }

    /**
     * SHA加密
     */
    public static String encryptSHA(String data) throws Exception {
        return encryptBASE64(SHACoder.encodeSHA256(data));
    }

    /**
     * RSA签名
     */
    public static String signRSA(String data, String privateKey) throws Exception {
        return RSACoder.sign(data.getBytes(UTF8), privateKey);
    }

    /**
     * RSA签名
     */
    public static String signRSA256(String data, String privateKey) throws Exception {
        return RSACoder.sign256(data.getBytes(UTF8), privateKey);
    }

    /**
     * RSA验签
     */
    public static boolean verifyRSA(String data, String publicKey, String sign) throws Exception {
        return RSACoder.verify(data.getBytes(UTF8), RSACoder.loadPublicKey(publicKey), sign);
    }

    /**
     * RSA验签
     */
    public static boolean verifyRSA256(String data, String publicKey, String sign) throws Exception {
        return RSACoder.verify256(data.getBytes(UTF8), RSACoder.loadPublicKey(publicKey), sign);
    }

    /**
     * HMAC加密
     *
     * @param type HmacMD2/HmacMD4/HmacMD5/HmacSHA1/HmacSHA224/HmacSHA256/HmacSHA512
     * @param data
     */
    public static String encryptHMAC(String type, String data) throws Exception {
        return encryptBASE64(HmacCoder.encodeHmac(type, data.getBytes(UTF8), HmacCoder.initHmacKey(type)));
    }

    /**
     * HMAC加密
     *
     * @param type HmacMD2/HmacMD4/HmacMD5/HmacSHA1/HmacSHA224/HmacSHA256/HmacSHA512
     * @param data
     */
    public static String encryptHMAC(String type, String data, String key) throws Exception {
        return encryptBASE64(HmacCoder.encodeHmac(type, data.getBytes(UTF8), key.getBytes(UTF8)));
    }

    /**
     * 生成6位随机盐值
     */
    public static String randomSalt() {
        return randomSalt(6);
    }

    /**
     * 生成随机盐值
     */
    public static String randomSalt(int capacity) {
        Random r = new Random();
        StringBuilder sb = new StringBuilder(capacity);
        sb.append(r.nextInt(999)).append(r.nextInt(999));
        int len = sb.length();
        if (len < capacity) {
            for (int i = 0; i < capacity - len; i++) {
                sb.append("0");
            }
        }
        return sb.toString();
    }
}
